package interchange.data;

import interchange.domain.Subasta;
import interchange.domain.Usuario;
import interchange.utiles.UIDGenerator;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class JDBCSubastaDAO implements ISubastaDAO {
	private Connection conn;
	private IUsuarioDAO iudao;
	private IPagoDAO ipdao;
	private IObjetoDAO iodao;
	
	public JDBCSubastaDAO() {
        conn = ConnectionManager.getInstance().checkOut();
        iudao = new JDBCUsuarioDAO();
        ipdao=new JDBCPagoDAO();
        
    }
	
	 protected void finalize() {
	        ConnectionManager.getInstance().checkIn(conn);
	    }
	 
	 
	public void delete(Connection conn, String IDSubasta) {
			String sql = "DELETE FROM Subasta WHERE (OID = ?) ";
	        PreparedStatement stmt = null;

	        String OIDSubasta = selectOIDOfSubasta(IDSubasta);
	        if (OIDSubasta!=null) {
	            
	            String OIDUsuario = selectOIDOfUsuario(OIDSubasta);
	            String OIDPago=selectOIDOfPago(OIDSubasta);
	            String OIDObjeto=selectOIDOfObjeto(OIDSubasta);


	            iudao.delete(conn, OIDUsuario);
	            ipdao.delete(conn,OIDPago);
	            iodao.delete(conn, OIDObjeto);


	            try {
	                stmt = conn.prepareStatement(sql);
	                stmt.setString(1, IDSubasta);
	                stmt.executeUpdate();
	            } catch (SQLException e) {
	                System.out.println("Message: " + e.getMessage());
	                System.out.println("SQLState: " + e.getSQLState());
	                System.out.println("ErrorCode: " + e.getErrorCode());
	            } finally {
	                try {
	                    if (stmt != null)
	                        stmt.close();
	                } catch (SQLException e) {
	                }
	            }
	            }

	        }
	
	


	public void insert(Connection conn, String OIDSubasta, Subasta a) {
		// Modificar campos BD
		
			String oids = UIDGenerator.getInstance().getKey();
			
			String oidu = UIDGenerator.getInstance().getKey();
	        iudao.insert(conn, oidu, a.getUsuarioCreador());
	        
	        String oidp = UIDGenerator.getInstance().getKey();
	        ipdao.insert(conn, oidp, a.getPago());
	        
	        String oido = UIDGenerator.getInstance().getKey();
	        iodao.insert(conn, oido, a.getObjeto());

			String sql = "INSERT INTO Subasta ( OIDSubasta, OIDUsuario, OIDPago, OIDObjeto, idSubasta, precio, fechaLimite, finalizada, confirmacion, fechaInicial) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
		
	        PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, oids);
	            stmt.setString(2, oidu);
	            stmt.setString(3, oidp);
	            stmt.setString(4, oido);
	            stmt.setString(5, a.getSubastaID());
	            stmt.setDouble(6, a.getPrecio());
	            stmt.setDate(7, (Date) a.getFechaLimite());
	            stmt.setBoolean(8, a.isFinalizado());
	            stmt.setBoolean(9,a.isConfirmado());
	            stmt.setDate(10,(Date)a.getFechaInicial() );
	            

	            stmt.executeUpdate();

	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

		}
	
	
	public Subasta select(Connection conn, String OIDSubasta) {
		  String sql = "SELECT * FROM Subasta WHERE (OIDSubasta = ? ) ";
	        PreparedStatement stmt = null;
	        ResultSet result = null;
	        Subasta u = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, OIDSubasta);

	            result = stmt.executeQuery();

	            result.next();
	            
	            String oido = result.getString("OIDObjeto");
                String oidu = result.getString("OIDUsuario");
                String oidp = result.getString("OIDPago");
	            u = (Subasta) new Subasta();
	            u.setConfirmado(new Boolean(result.getString("confirmado")));
	            u.setFechaInicial(new Date(result.getLong("fechaInicial")));
	            u.setFechaLimite(new Date(result.getLong("fechaLimite")));
	            u.setFinalizado(new Boolean(result.getString("finalizado")));
	            u.setPrecio(new Double(result.getString("precio")));
	            u.setObjeto(iodao.select(conn, oido));
	            u.setPago(ipdao.select(conn,oidp));
	            u.setUsuarioCreador(iudao.select(conn,oidu));
	            
	   
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }

	        return u;
	    }
	
	
	private String selectOIDOfObjeto(String OIDSubasta) {
		PreparedStatement stmt = null;
	    ResultSet result = null;
	    String oid = null;
	    String sql = "SELECT * FROM Subasta WHERE (OIDSubasta = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, OIDSubasta);
	            result = stmt.executeQuery();

	            if (result.next()) {
	                oid = result.getString("OIDObjeto");
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oid;
	}

	private String selectOIDOfPago(String OIDSubasta) {
		PreparedStatement stmt = null;
	    ResultSet result = null;
	    String oid = null;
	    String sql = "SELECT * FROM Subasta WHERE (OIDSubasta = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, OIDSubasta);
	            result = stmt.executeQuery();

	            if (result.next()) {
	                oid = result.getString("OIDPago");
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oid;
	}

	private String selectOIDOfUsuario(String OIDSubasta) {
		PreparedStatement stmt = null;
	    ResultSet result = null;
	    String oid = null;
	    String sql = "SELECT * FROM Subasta WHERE (OIDSubasta = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, OIDSubasta);
	            result = stmt.executeQuery();

	            if (result.next()) {
	                oid = result.getString("OIDUsuario");
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oid;

	}

	private String selectOIDOfSubasta(String iDSubasta) {
		PreparedStatement stmt = null;
	    ResultSet result = null;
	    String oid = null;
	    String sql = "SELECT * FROM Subasta WHERE (OIDSubasta = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, iDSubasta);
	            result = stmt.executeQuery();

	            if (result.next()) {
	                oid = result.getString("OIDSubasta");
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oid;
			return null;
	}
}
